#ifndef PAGENUMBERPREVIEWWIDGET_H
#define PAGENUMBERPREVIEWWIDGET_H

#include <QWidget>
#include <QPaintEvent>
#include <QPainter>
#include <QTimer>
#include "processingsettings.h"

class PageNumberPreviewWidget : public QWidget
{
    Q_OBJECT

public:
    explicit PageNumberPreviewWidget(QWidget *parent = nullptr);

    void setPageNumberSettings(const ProcessingSettings::PageNumberSettings& settings);
    ProcessingSettings::PageNumberSettings getPageNumberSettings() const;

    void setSamplePage(int currentPage, int totalPages);
    void setShowGrid(bool show);
    void setAutoRefresh(bool enabled);

public slots:
    void refreshPreview();

protected:
    void paintEvent(QPaintEvent *event) override;
    void resizeEvent(QResizeEvent *event) override;

private:
    void drawBackground(QPainter& painter);
    void drawPageContent(QPainter& painter);
    void drawPageNumber(QPainter& painter);
    void drawGrid(QPainter& painter);

    // 计算页码位置
    QPointF calculatePageNumberPosition(const QSizeF& pageSize) const;
    QSizeF getPageSize() const;

    // 绘制相关
    QRectF getContentRect() const;
    void applyPageNumberTransform(QPainter& painter, const QPointF& position) const;

    ProcessingSettings::PageNumberSettings m_settings;
    int m_currentPage = 1;
    int m_totalPages = 10;
    bool m_showGrid = false;
    bool m_autoRefresh = true;

    // 预览相关
    QTimer* m_refreshTimer;
    static constexpr int REFRESH_DELAY_MS = 100;
};

#endif // PAGENUMBERPREVIEWWIDGET_H